package com.gopay.bis.tbank.expay.operation;

import java.math.BigDecimal;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.gopay.common.domain.bank.tbank.expay.PayResult;
import com.gopay.common.domain.bank.tbank.expay.AgrmtResult;
import com.gopay.common.domain.bank.tbank.expay.Result;
import com.gopay.bis.common.util.XMLBeanUtils;
import com.gopay.common.domain.bank.tbank.expay.TBankMessageExPay;

/**
 * 
 * @ExPayParserMessage.java
 * @author WangHongFei
 */
public class ExPayParserMessage {

    /**
     * 解析tempObj(agree) toString()
     * 
     * @param Object
     * @return TBankMessageExPay
     */
    public TBankMessageExPay parseExpayAgree(Object tempObj) throws Exception{
        String tempStr = tempObj.toString();
        TBankMessageExPay mtPayMessage = null;
        Map<String, Class> cm = new ConcurrentHashMap<String, Class>();
        cm.put("agrmtResult", AgrmtResult.class);
        AgrmtResult agrmtResult = (AgrmtResult) XMLBeanUtils.xml2Bean(cm, tempStr);
        mtPayMessage = msg4ExPayAgree(agrmtResult);
        return mtPayMessage;
    }

    private TBankMessageExPay msg4ExPayAgree(AgrmtResult agrmtResult) throws Exception{
        TBankMessageExPay mtPayMessage = new TBankMessageExPay();
        ((Map) mtPayMessage.getExPayHashMap()).put("retCd", agrmtResult.getRetCd());
        ((Map) mtPayMessage.getExPayHashMap()).put("retMsg", agrmtResult.getRetMsg());
        mtPayMessage.setAgrmtNo(agrmtResult.getAgrmtNo());
        return mtPayMessage;
    }

    /**
     * 解析tempObj(order) toString()
     * 
     * @param Object
     * @return TBankMessageExPay
     */
    public TBankMessageExPay parseExpayOrder(Object tempObj) throws Exception{
        String tempStr = tempObj.toString();
        TBankMessageExPay mtPayMessage = null;
        Map<String, Class> cm = new ConcurrentHashMap<String, Class>();
        cm.put("payResult", PayResult.class);
        PayResult payResult = (PayResult) XMLBeanUtils.xml2Bean(cm, tempStr);
        mtPayMessage = msg4ExPayOrder(payResult);
        return mtPayMessage;
    }

    private TBankMessageExPay msg4ExPayOrder(PayResult payResult) throws Exception{
        TBankMessageExPay mtPayMessage = new TBankMessageExPay();
        ((Map) mtPayMessage.getExPayHashMap()).put("retCd", payResult.getRetCd());
        ((Map) mtPayMessage.getExPayHashMap()).put("retMsg", payResult.getRetMsg());
        mtPayMessage.setAgrmtNo(payResult.getAgrmtNo());
        mtPayMessage.setAmount(new BigDecimal(payResult.getOrderAmount()));
        mtPayMessage.setOrderId(payResult.getOrderNo());
        return mtPayMessage;

    }

    /**
     * 查询
     * 
     * @param Object
     * @return TBankMessageExPay
     */
    public TBankMessageExPay parseExpayOrderQuery(Object tempObj) throws Exception{
        String tempStr = tempObj.toString();
        TBankMessageExPay mtPayMessage = null;
        Map<String, Class> cm = new ConcurrentHashMap<String, Class>();
        cm.put("result",Result.class);
        Result result = (Result) XMLBeanUtils.xml2Bean(cm, tempStr);
        mtPayMessage = msg4ExPayOrderQurey(result);
        return mtPayMessage;
    }

    private TBankMessageExPay msg4ExPayOrderQurey(Result result) throws Exception{
        TBankMessageExPay mtPayMessage = new TBankMessageExPay();
        ((Map) mtPayMessage.getExPayHashMap()).put("retCd", result.getBody().getOrders().getOrder().getRetCd());
        ((Map) mtPayMessage.getExPayHashMap()).put("retMsg", result.getBody().getOrders().getOrder().getRetMsg());
        mtPayMessage.setOrderId(result.getBody().getOrders().getOrder().getOrderNo());
        mtPayMessage.setAmount(new BigDecimal(result.getBody().getOrders().getOrder().getOrderAmount()));
        return mtPayMessage;
    }
}
